import React from "react";
import { sleep } from 'antd-mobile/es/utils/sleep'
import styles from '@/assets/css/order.module.css';

export default () =>
{
    const [list, SetList] = React.useState<any>([])
    const [hasMore, SethasMore] = React.useState(false)
    const [page, SetPage] = React.useState(1)
    const [status, SetStatus] = React.useState('all')
    let [RateShow, SetRateShow] = React.useState(false)
    let [form] = React.UI.Form.useForm()
    let [comment] = useState({rate: 5, content: ''})
    let [orderid, SetID] = useState(0)

    React.useEffect(() => {
        GetOrderData()
    }, [])

    //给状态单独检测变化钩子函数
    React.useEffect(() => {
        // 下拉刷新
        OnRefresh()
    }, [status])

    //请求数据的方法
    const GetOrderData = async () => 
    {
        let result = await React.request.post('order/OrderIndex', {
            busid: React.business.id,
            page: page,
            status: status
        })

        if (result.code == 0) 
        {
            //没有更多数据
            SethasMore(false);
            return false
        }

        //设置分页和列表合并
        SethasMore(true)
        SetList(list.concat(result.data))
        SetPage(page + 1)
    }

    //下拉刷新
    const OnRefresh = async () => 
    {

        SetPage(1)
        SethasMore(true);
        SetList([]);

        await sleep(1000);
        await GetOrderData();
    };

    //上拉加载
    const LoadMore = async () => 
    {
        await sleep(1000)
        GetOrderData()
    }

    //申请退款
    const ApplyRefund = async (orderid: any) =>
    {
        let res = await React.UI.Dialog.confirm({
            title: '退款提醒',
            content: '是否确认申请退款'
        })
        
        if(!res) return false

        //组装参数
        var params = {
            busid: React.business.id,
            orderid
        }

        //发送请求
        var result = await React.request.post('/order/ApplyRefund', params)

        if(result.code == 0)
        {
           return React.toast(result.msg)
        }

        React.toast(result.msg, () => {
            //刷新页面
            OnRefresh()
        })
    }

    //评价方法
    const CommentChange = (orderid: any) =>
    {
        SetID(orderid)
        SetRateShow(true)
    }

    //按钮列表
    const ButtonList = (order: any) => 
    {
        if(order.status == 1)  //已支付
        {
            return (
                <React.UI.Button color='danger' size='small' onClick={() => ApplyRefund(order.id)}>申请退款</React.UI.Button>
            )
        }

        //已退房，但是还没评价
        if(order.status == '3' && !order.rate)
        {
            return (<React.UI.Button color='warning' size='small' onClick={() => CommentChange(order.id)}>立即评价</React.UI.Button>)
        }

        if(order.status == '-1')
        {
            return (<React.UI.Button color="warning" size="small">申请退款中...</React.UI.Button>)
        }
    }

    //提交表单
    const submit = async (values: any) =>
    {
        SetRateShow(false)

        values.orderid = orderid
        values.busid = React.business.id

        console.log(values)
        return false
        
        var result = await React.request.post('/order/OrderRate', values)

        if(result.code == 0)
        {
            return React.toast(result.msg)
        }

        React.toast(result.msg, () => {
            //刷新页面
            OnRefresh()
        })
    }


    return (
        <>
            {/* 选项卡 */}
            <React.UI.Tabs activeKey={status} activeLineMode={'full'} onChange={(val:string) => SetStatus(val)}>
                <React.UI.Tabs.Tab title="全部" key="all" />
                <React.UI.Tabs.Tab title="已支付" key="1" />
                <React.UI.Tabs.Tab title="已入住" key="2" />
                <React.UI.Tabs.Tab title="已退房" key="3" />
                <React.UI.Tabs.Tab title="已评价" key="4" />
                <React.UI.Tabs.Tab title="售后" key="-1" />
            </React.UI.Tabs>

            {/* 下拉刷新 */}
            <React.UI.PullToRefresh onRefresh={OnRefresh}>
                {/* 订单列表 */}
                <div className={styles.order_list}>
                    {list && list.map((item: any, key:number) => 
                        <div className={styles.item} key={key}>
                            <div className={styles.item_top}>
                                <p>{item.room.name}</p>
                                <div className={styles.top_tag}>{item.status_text}</div>
                            </div>

                            <div className={styles.house}>
                                <div className={styles.item_swipers}>
                                    <img src={item.room.thumb_text} alt="" />
                                </div>
                                <div className={styles.item_times}>
                                    <div>
                                        <div>{item.starttime_text}</div>
                                        <div>{item.startday_text}</div>
                                    </div>
                                    <div>
                                        <div>共{item.order_day}晚</div>
                                        <div className={styles.item_right}>
                                            <img src={require("@/assets/images/right1.png")} alt="" />
                                        </div>
                                    </div>
                                    <div>
                                        <div>{item.endtime_text}</div>
                                        <div>{item.endday_text}</div>
                                    </div>
                                    <div>
                                        <div className={styles.item_pay}>订单总价</div>
                                        <div className={styles.item_price}>￥{item.price}</div>
                                    </div>
                                </div>
                            </div>

                            <div className={styles.item_bar}>
                                {ButtonList(item)}

                                <React.UI.Button color='primary' size='small' onClick={() => React.navigate(`/order/info?orderid=${item.id}`)}>查看详情</React.UI.Button>
                            </div>
                        </div>
                    )}
                </div>

                {/* 上拉加载 */}
                <React.UI.InfiniteScroll loadMore={LoadMore} hasMore={hasMore} threshold={0} />
            </React.UI.PullToRefresh>

            <React.UI.Popup
                visible={RateShow}
                onMaskClick={() => SetRateShow(false)}
                onClose={() => SetRateShow(false)}
            >
                <React.UI.Form
                    form={form}
                    name="comment"
                    layout="horizontal"
                    mode="card"
                    initialValues={comment}
                    onFinish={submit}
                    footer={<React.UI.Button block type='submit' color='primary' size='large'>提交</React.UI.Button>}
                >
                    <React.UI.Form.Item name="content" label="评价内容" rules={[{required: true, message: '请输入评价内容'}]}>
                        <React.UI.TextArea maxLength={100} rows={2} showCount placeholder="请输入评价内容" />
                    </React.UI.Form.Item>

                    <React.UI.Form.Item name="rate" label="评分" rules={[{required: true, message: '请选择评分'}]}>
                        <React.UI.Rate />
                    </React.UI.Form.Item>
                </React.UI.Form>

            </React.UI.Popup>
        </>
    )
}